package com.example.demo.service;

import com.alibaba.fastjson.JSON;
import com.example.demo.dao.pojo.UserInfo;
import com.example.demo.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class TokenService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public UserInfo checkToken(String token){
        if(!StringUtils.hasLength(token)){
            return null;
        }
        Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);
        if(stringObjectMap == null){
            return null;
        }

        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
        if(!StringUtils.hasLength(userJson)){
            return null;
        }
        UserInfo userInfo = JSON.parseObject(userJson, UserInfo.class);
        return userInfo;
    }
}
